package org.go.ee.jmx.middle.localhost;

import java.io.IOException;
import java.rmi.registry.LocateRegistry;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXServiceURL;

public class RmiAgent {

	private Map<String, String> strs = null;
	private int port = 1099;
	private JMXConnectorServer connectorServer;
	private MBeanServer mbs;

	public RmiAgent(Map<String, String> strs, int port) {
		this.strs = strs;
		this.port = port;
	}

	public void start() throws Exception {
		this.mbs = MBeanServerFactory.createMBeanServer("GoDefaultJMX");
		this.connectorServer = startRMIConnector(mbs);
		ObjectName connectorName1 = new ObjectName("GoRmiAgent:name=RMIConnector");
		mbs.registerMBean(connectorServer, connectorName1);
		Iterator<String> iterators = strs.keySet().iterator();
		while (iterators.hasNext()) {
			String key = iterators.next();
			ObjectName objName = new ObjectName(key);
			mbs.registerMBean(Class.forName(strs.get(key)).newInstance(), objName);

		}
	}

	//Listing 3.3 startRMIConnector() method that adds the RMI  connector  server to the agent
	protected JMXConnectorServer startRMIConnector(MBeanServer server) throws IOException {
		LocateRegistry.createRegistry(port);
		//--
		//MBeanServer server = MBeanServerFactory.createMBeanServer();
		//		ObjectName namingName = ObjectName.getInstance("naming:type=rmiregistry");
		//		server.createMBean("mx4j.tools.naming.NamingService", namingName, null);
		//		server.setAttribute(namingName, new Attribute("Port", new Integer(2099)));
		//		server.invoke(namingName, "start", null, null);
		//--
		//javax.management.remote.rmi.RMIConnectorServer
		//String urlStr = "service:jmx:rmi://localhost/jndi/rmi://" + InetAddress.getLocalHost().getHostAddress() + ":" + 2099 + "/jmxconnector";
		//service:jmx:rmi:///jndi/rmi://localhost:8077/jmxconnector
		String urlStr = "service:jmx:rmi:///jndi/rmi://" + "127.0.0.1" + ":" + String.valueOf(port) + "/jmxconnector";
		JMXServiceURL url = new JMXServiceURL(urlStr);
		HashMap environment = new HashMap();
		javax.management.remote.rmi.RMIConnectorServer connector = new javax.management.remote.rmi.RMIConnectorServer(url, environment, server);
		//JMXConnectorServer jMXConnectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, environment, server);
		//jMXConnectorServer.start();
		connector.start();
		return connector;
	}

	public void unRegistry() throws Exception {
		if (this.connectorServer != null) {
			this.connectorServer.stop();
		}
		if (this.mbs != null) {
			MBeanServerFactory.releaseMBeanServer(this.mbs);
		}
	}
}

/*
this.server = MBeanServerFactory.createMBeanServer();
ObjectName namingName = ObjectName.getInstance("naming:type=rmiregistry");
this.server.createMBean("mx4j.tools.naming.NamingService", namingName, null);
this.server.setAttribute(namingName, new Attribute("Port", new Integer(port)));
this.server.invoke(namingName, "start", null, null);
this.connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, environment, this.server);
this.connectorServer.start();
MBeanServerFactory.releaseMBeanServer(this.server);
 
public void unregistry() throws Exception                                                                                    
{                                                                                                     
  if (this.connectorServer != null) {                                                                 
    this.connectorServer.stop();                                                                      
  }                                                                                                   
  if (this.server != null)                                                                            
    MBeanServerFactory.releaseMBeanServer(this.server);                                               
}
*/
